package com.example.demotest.controller;

import com.example.demotest.pojo.Complaint;
import com.example.demotest.pojo.ResponseMessage;
import com.example.demotest.pojo.dto.ComplaintDto;
import com.example.demotest.pojo.dto.ComplaintUpdateDto;
import com.example.demotest.service.IComplaintService;
import com.example.demotest.util.JWT;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/complaints")
public class ComplaintController {

    @Autowired
    IComplaintService complaintService;

    @Autowired
    private JWT jwt;

    /*
     * 提交投诉
     */
    @PostMapping("/submit")
    public ResponseMessage<Object> submitComplaint(HttpServletRequest request, @Valid @RequestBody ComplaintDto complaintDto) {
        String token = jwt.extractToken(request);
        if (token == null || !jwt.validateToken(token)) {
            return ResponseMessage.error(401,"请先登录");
        }
        Integer userId = jwt.getUserIdFromToken(token);
        complaintService.submitComplaint(userId, complaintDto);
        return ResponseMessage.success("投诉已提交");
    }

    /*
     * 获取全部投诉
     */
    @GetMapping
    public List<Complaint> getAllComplaints() {
        return complaintService.getAllComplaints();
    }

    @GetMapping("/{complaintId}")
    public Complaint getComplaintByComplaint(@PathVariable Integer complaintId) {
        return complaintService.getComplaintsByComplaint(complaintId);
    }

    /*
     * 获取某个用户的投诉信息
     */
    @GetMapping("/complainant/{userId}")
    public List<Complaint> getComplaintsByComplainant(@PathVariable Integer userId) {
        return complaintService.getComplaintsByComplainant(userId);
    }

    /*
     * 获取某个用户全部被投诉信息
     */
    @GetMapping("/defendant/{userId}")
    public List<Complaint> getComplaintsByDefendant(@PathVariable Integer userId) {
        return complaintService.getComplaintsByDefendant(userId);
    }

    /*
     * 获取某个商品的被投诉信息
     */
    @GetMapping("/product/{productId}")
    public List<Complaint> getComplaintsByProduct(@PathVariable Integer productId) {
        return complaintService.getComplaintsByProduct(productId);
    }


    @PutMapping("/admin/handle")
    public ResponseMessage<Object> adminUpdateComplaint(HttpServletRequest request, @RequestBody ComplaintUpdateDto updateDto) {
        String token = jwt.extractToken(request);
        if (token == null || !jwt.validateToken(token)) {
            return ResponseMessage.error(401,"请先登录");
        }
        Integer userId = jwt.getUserIdFromToken(token); // 这里需要验证管理员身份
        complaintService.adminUpdateComplaint(updateDto);
        return ResponseMessage.success("修改成功");
    }

}
